package org.jetnuke.servlets.utilities;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.ServletContext;

public class GetResources {
	private GetResources() {
	};

	public static List getResources(ServletContext sc, String initPath) {
		List resList = null;
		String path = sc.getRealPath(initPath);
		File f = new File(path);
		String strs[] = f.list();
		resList = new ArrayList(Arrays.asList(strs));
		return resList;
	}
	public static List getDirectories(ServletContext sc, String initPath) {
		List resList = null;
		String path = sc.getRealPath(initPath);
		File f = new File(path);
		String strs[] = f.list();
		resList = new ArrayList();
		if(strs!=null){
			for(int i=0;i<strs.length;i++){
				File dir=new File(path,strs[i]);
				if(dir.isDirectory())
					resList.add(strs[i]);
			}
		}
		return resList;
	}
	public static List getFileNames(ServletContext sc, String initPath) {
		List resList = null;
		String path = sc.getRealPath(initPath);
		File f = new File(path);
		String strs[] = f.list();
		resList = new ArrayList();
		if(strs!=null){
			for(int i=0;i<strs.length;i++){
				File dir=new File(path,strs[i]);
				if(!dir.isDirectory())
					resList.add(strs[i]);
			}
		}
		return resList;
	}
	public static List getFiles(ServletContext sc, String initPath) {
		List resList = null;
		String path = sc.getRealPath(initPath);
		File f = new File(path);
		File files[] = f.listFiles();
		resList = new ArrayList();
		if(files!=null){
			for(int i=0;i<files.length;i++){
				if(!files[i].isDirectory())
					resList.add(files[i]);
			}
		}
		return resList;
	}
}
